package com.wondersgroup.insurance.dnuilearn.views.shader;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ComposeShader;
import android.graphics.LinearGradient;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.PorterDuff;
import android.graphics.Shader;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.AppCompatImageView;
import android.util.AttributeSet;
import android.view.View;

import com.wondersgroup.insurance.dnuilearn.R;

/**
 * Created by Administrator on 2017/5/12.
 */

public class InvertedImage extends View {
    private int[] mColors = {Color.WHITE,Color.TRANSPARENT,};

    Bitmap invertedBitmap= null;
    Bitmap oriBmp = null;
    Paint paint = null;
    public InvertedImage(Context context) {
        this(context, null);
    }

    public InvertedImage(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }



    public InvertedImage(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);


        oriBmp = BitmapFactory.decodeResource(getResources(), R.drawable.xyjy3);
        Matrix matrix = new Matrix();
        matrix.setScale(1,-1);
        invertedBitmap = Bitmap.createBitmap(oriBmp,0,0,oriBmp.getWidth(),oriBmp.getHeight(),matrix,false);

        paint = new Paint();
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (oriBmp != null){
            canvas.drawBitmap(oriBmp,0,0,paint);
        }

        if (invertedBitmap != null){
            BitmapShader bmpShader = new BitmapShader(invertedBitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
            Matrix matrix = new Matrix();
            matrix.setTranslate(0,oriBmp.getHeight()+20);
            bmpShader.setLocalMatrix(matrix);
            LinearGradient linearGradient = new LinearGradient( 0, invertedBitmap.getHeight()+20,0, invertedBitmap.getHeight()*2+20, mColors, null, Shader.TileMode.CLAMP);
            ComposeShader composeShader = new ComposeShader(bmpShader,linearGradient, PorterDuff.Mode.MULTIPLY);
            paint.setShader(composeShader);
            canvas.drawRect(0,invertedBitmap.getHeight()+20, invertedBitmap.getWidth(),invertedBitmap.getHeight()*2+20,paint);
        }


    }


}
